package com.sobjal.android;

import java.util.List;

import com.sobjal.android.devices.Device;
import com.sobjal.android.utils.SmartObjects;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;

/**
 * Right after user logged in ;
 * 
 * @author Fabio Monteiro, Chen Lu
 */
public class login extends Activity {

	private List<Device> courant = null;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		this.setTitle("Welcome");
		courant = SmartObjects.SMART_OBJECTS_LIST;
		
		ImageView image = (ImageView) findViewById(R.id.ImageView01);
		Animation hyperspaceJump = AnimationUtils.loadAnimation(this,
				R.anim.myanim);
		image.startAnimation(hyperspaceJump);

		
	}

public void actionOneClickSeveral(View oneView) {

		Intent intent = new Intent();
		intent.setClass(login.this, listSeveral.class);
		startActivity(intent);

	}

	//action button select one device
	public void actionClickSelectOneDevice(View view){
		final CharSequence[] items = {"By name of device", "By type of device"};

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Select");
		builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int item) {
		        if(item ==0){
		        	Intent intent = new Intent();
					intent.setClass(login.this, listSmart.class);
					startActivity(intent);
		        }
		        if(item==1){
		        	Intent intent = new Intent();
					intent.setClass(login.this, listType.class);
					startActivity(intent);

		        }
		    }
		}).show();
	}

// action for menu

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		 
		LayoutInflater  factory = LayoutInflater.from(this);
		final View textEntryView = factory.inflate(R.layout.dialog_about, null);
		 
		switch (item.getItemId()) {
		case R.id.menulogin:
			// about function
			if (SmartObjects.SMART_OBJECTS_LIST.isEmpty()) {
				Intent intent = new Intent();
				intent.setClass(login.this, SobjalMain.class);
				startActivity(intent);
			} else {
			}
			return true;
		case R.id.menulogoff:
			// settings function
			new AlertDialog.Builder(this)
					.setMessage("Are you sure you want to exit?")
					.setPositiveButton("Yes",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									SmartObjects.SMART_OBJECTS_LIST = null;
									Intent intent = new Intent();
									intent.setClass(login.this,
											SobjalMain.class);
									startActivity(intent);
									login.this.finish();
								}
							})
					.setNegativeButton("No",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.cancel();
								}
							}).show();
			return true;
		case R.id.menuabout:
			
			new AlertDialog.Builder(this)
			.setMessage("About")
			.setIcon(android.R.drawable.ic_menu_info_details)
			.setView(textEntryView).show();
		default:
			Log.e("Unknown menu item selected", " ");
			return false;
		}
	}
}
